package repository

import (
	"energy-amiot-platform/model/dbspo"

	"gorm.io/gorm"
)

var DEVICE deviceRepository

type deviceRepository struct {
}

func (d *deviceRepository) Exist(db *gorm.DB, id string) (*gorm.DB, bool) {
	var count int64
	return db.Model(&dbspo.Device{}).Where("id=?", id).Count(&count), count == 1
}

func (d *deviceRepository) ExistByDeviceId(db *gorm.DB, deviceId string) (*gorm.DB, bool) {
	var count int64
	return db.Model(&dbspo.Device{}).Where("device_id=?", deviceId).Count(&count), count == 1
}

func (d *deviceRepository) FindByDeviceId(db *gorm.DB, deviceId string) (*gorm.DB, *dbspo.Device) {
	var device dbspo.Device
	return db.Find(&device, "device_id=?", deviceId), &device
}

func (d *deviceRepository) CountByProductId(db *gorm.DB, productId uint64) (*gorm.DB, *int64) {
	var count int64
	return db.Model(&dbspo.Device{}).Where("cid=?", productId).Count(&count), &count
}

func (d *deviceRepository) Create(db *gorm.DB, device *dbspo.Device) *gorm.DB {
	return db.Create(device)
}

func (d *deviceRepository) DeleteByDevId(db *gorm.DB, deviceId string) *gorm.DB {
	return db.Delete(&dbspo.Device{}, "device_id=?", deviceId)
}
